package ex8;

import java.util.ArrayList;
import java.util.Map.Entry;
import java.util.Scanner;


import java.util.TreeMap;
import java.util.TreeSet;

public class Test2 {
	public static void main(String[] args) {
		ArrayList<String> array = new ArrayList<String>();
 		TreeMap<String, TreeSet<Integer>> treeMap = new TreeMap<String, TreeSet<Integer>>();
		Scanner scanner = new Scanner(System.in);
		int cnt = 1;
		while (scanner.hasNextLine()) {
			String string = scanner.nextLine();
			if (string.equals("!!!!!")) {
				break;
			}
			array.add(string);
			Scanner scanner2 = new Scanner(string);
			while (scanner2.hasNext()) {
				String string2 = scanner2.next();
				TreeSet<Integer> tmp = treeMap.get(string2);
				if (tmp == null) {
					tmp = new TreeSet<Integer>();
				}
				tmp.add(cnt);
				treeMap.put(string2, tmp);
			}
			scanner2.close();
			cnt++;
		}
		
		for (Entry<String, TreeSet<Integer>> entry : treeMap.entrySet()) {
			System.out.println(entry.getKey() + "=" + entry.getValue());
		}
		
		
		while (scanner.hasNextLine()) {
			String string = scanner.nextLine();
			if (string.length() == 0) {
				System.out.println("found 0 results");
				continue;
			}
			Scanner scanner2 = new Scanner(string);
			TreeSet<Integer> tmp = treeMap.get(scanner2.next());
			if (tmp == null) {
				System.out.println("found 0 results");
				continue;
			}
			while (scanner2.hasNext()) {
				String string2 = scanner2.next();
				tmp.retainAll(treeMap.get(string2));
			}
			scanner2.close();
			
			if (tmp.isEmpty()) {
				System.out.println("found 0 results");
			} else {
				System.out.println(tmp);
				for (Integer integer : tmp) {
					System.out.println("line " + integer + ":" + array.get(integer - 1));
				}
				
			}
		}
		scanner.close();
	}
}